package com.future.mayi.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.future.baselib.utils.CommonUtils;
import com.future.mayi.R;
import com.future.mayi.model.SpecModel;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SkuEdtNumDialog extends Dialog implements DialogInterface.OnDismissListener {
    @BindView(R.id.iv_add)
    ImageView ivAdd;
    @BindView(R.id.edt)
    EditText edt;
    @BindView(R.id.iv_sub)
    ImageView ivSub;
    @BindView(R.id.ll_view)
    LinearLayout llView;

    private Context mContext;

    public SkuEdtNumDialog(@NonNull Context context) {
        super(context, R.style.transparentDialog);
        mContext = context;
        Window window = getWindow();
        if (window == null) {
            return;
        }
        WindowManager.LayoutParams wmlp = window.getAttributes();
        int[] xy = CommonUtils.getScreenSize(getContext());
        wmlp.width = xy[0];
        wmlp.height = xy[1];
        window.setAttributes(wmlp);

        View layout = LayoutInflater.from(context).inflate(R.layout.dialog_sku_edt_num, null);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(xy[0], xy[1]);
        setContentView(layout, params);

        ButterKnife.bind(this);
        setOnDismissListener(this);
    }

    @OnClick({R.id.ll_view, R.id.iv_add, R.id.iv_sub, R.id.rl_main})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_add: {
                int num = 0;
                if (!TextUtils.isEmpty(edt.getText())) {
                    num = Integer.valueOf(edt.getText().toString());
                }
                num = num + 1;
                edt.setText(String.valueOf(num));
                edt.setSelection(String.valueOf(num).length());
                ivSub.setAlpha(num <= 1 ? 0.2f : 1.0f);
            }
            break;
            case R.id.iv_sub: {
                int num = 0;
                if (!TextUtils.isEmpty(edt.getText())) {
                    num = Integer.valueOf(edt.getText().toString());
                }
                if (num <= 1) return;
                num = num - 1;
                edt.setText(String.valueOf(num));
                edt.setSelection(String.valueOf(num).length());
                ivSub.setAlpha(num <= 1 ? 0.2f : 1.0f);
            }
            break;
            case R.id.rl_main:
                dismiss();
                break;
            default:
                break;
        }
    }


    private View view;

    public void show(View view) {

        if (view.getTag() == null) return;
        if (!(view.getTag() instanceof SpecModel)) return;
        super.show();
        llView.setVisibility(View.INVISIBLE);
        this.view = view;
        SpecModel specModel = (SpecModel) view.getTag();
        edt.setText(String.valueOf(specModel.getNum()));
        edt.setSelection(String.valueOf(specModel.getNum()).length());
        ivSub.setAlpha(specModel.getNum() <= 1 ? 0.2f : 1.0f);
        View viewLine = view.findViewById(R.id.view_line);
        int[] location = new int[2];
        viewLine.getLocationOnScreen(location);


        llView.setVisibility(View.VISIBLE);
        llView.setX(location[0]);
        llView.setY(location[1] - CommonUtils.dip2px(mContext, 105.5f));
        llView.invalidate();
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        if (llView != null)
            llView.setVisibility(View.INVISIBLE);
        if (view == null) return;
        SpecModel specModel = (SpecModel) view.getTag();
        int num = 1;
        if (!TextUtils.isEmpty(edt.getText())) {
            num = Integer.valueOf(edt.getText().toString());
        }
        if (num < 1) num = 1;
        specModel.setNum(num);
        TextView tvName = view.findViewById(R.id.tv_name);
        tvName.setText(specModel.getGoods_spec() + (" *" + specModel.getNum()));
    }
}
